<template>
  <div style="margin-bottom: 55px;">
    <h1 v-if="article != null && article.isWechat != 1 ">{{ article.title }}</h1>
    <p v-if="article != null" v-html="article.content">{{ article.content }}</p>
  </div>
</template>

<script>
import weixinShare from '../../assets/share.js';
import $ from 'jquery';
export default {
  name: 'articleDetail',
  data() {
    return {
      articleId: '',
      userArticleId: '',
      article: null,
      oldId: '',
      openid: '',
      code: '',
      articleLog: '0',
      readTime: 0,
      reload: false,
    };
  },
  created() {
    if (localStorage.getItem('openId') == null) {
      // 如果缓存localStorage中没有微信openId，则需用code去后台获取
      console.log('获取localStorage失败，准备获取code');
      this.getCode();
    } else {
      if (this.openid === '') {
        // this.openid = this.util.getLocalStorage('openid');
        this.openid = localStorage.getItem('openId');
      }
      if(localStorage.getItem('nickname') == null){
        var that = this
        $.ajax({
          url: 'https://yokapp.mynatapp.cc/user/getName',
          type: 'GET',
          async: false,
          data: { openid: that.openid },
          success: function(data) {
            console.log('getInfo成功' + data);
            localStorage.setItem('nickname', data.nickname);
            localStorage.setItem('headImgUrl', data.headImgUrl);
          },
          error: function(xhr, textStatus) {
            console.log('getInfo失败' + textStatus);
          }
        });
      }
    }

    console.log('getLocalStorage');
    var that = this;
    this.articleId = this.$route.query.articleId;
    this.userArticleId = this.$route.query.userArticleId;
    if(this.$route.query.oldId != null && this.$route.query.oldId != ''){
      this.oldId = this.$route.query.oldId;
    } else{
      this.oldId = this.userArticleId;
    }
    if (this.$route.query.article == null || this.$route.query.article.id == null) {
      $.ajax({
        url: 'https://yokapp.mynatapp.cc/article/getArticle',
        type: 'GET',
        async: false,
        data: { articleId: that.articleId, userArticleId: this.userArticleId, openid: this.openid },
        success: function(data) {
          console.log('getArticle成功');
          that.article = data.article;
          that.userArticleId = data.userArticleId;
          if(data.userArticleId != data.oldId){
            that.oldId = data.oldId;
            that.$router.push({
              path: '/articleDetail',
              query: { articleId: that.article.id, userArticleId: that.userArticleId,oldId: that.oldId, article: that.article }
            });
            that.reload = true;
            location.reload();
          } else{
            if(that.oldId != that.userArticleId && ! that.reload){
              that.setLog();
            }
          }
          // console.log("oldId:" + that.oldId + "uaId:" + that.userArticleId)
          // window.alert("oldId:" + that.oldId + "uaId:" + that.userArticleId)
        },
        error: function(xhr, textStatus) {
          console.log('getArticle失败' + textStatus);
        }
      });
    } else {
      this.article = this.$route.query.article;
      this.oldId = this.$route.query.oldId;
    }
    if (this.article != null) {
      // if(this.oldId != this.userArticleId && ! this.reload){
      //   this.setLog();
      // }
      var flags = (this.oldId == this.userArticleId)
      weixinShare(this.article, this.openid, this.oldId, this.userArticleId, flags);
      if (this.articleLog != '0') {
        setInterval(function() {
          that.readTime = that.readTime+1;
          $.ajax({
            url: 'https://yokapp.mynatapp.cc/wechat/time',
            type: 'GET',
            async: true,
            data: { readTime: that.readTime, articleLog: that.articleLog},
            success: function(data) {
              console.log(data);
            },
            error: function(xhr, textStatus) {
              console.log('gettime失败' + textStatus);
            }
          });
        }, 60000);
      }
    }
  },
  methods: {
    setLog: function() {
      var that = this;
      $.ajax({
        url: 'https://yokapp.mynatapp.cc/article/articleLog',
        type: 'GET',
        async: false,
        data: { userArticleId: that.oldId, articleId: that.articleId, openid: that.openid },
        success: function(data) {
          that.articleLog = data;
          console.log('articleLog成功' + data);
        },
        error: function(xhr, textStatus) {
          console.log('articleLog失败' + textStatus);
        }
      });

      // if (this.userArticleId != this.oldId) {
      //   // this.$router.push({
      //   //   query: { articleId: this.articleId, userArticleId: this.userArticleId, article: this.article }
      //   // });
      //   that.$router.push({
      //     path: '/articleDetail',
      //     query: { articleId: that.article.id, userArticleId: that.userArticleId, article: that.article }
      //   });
      //   window.alert("push uaId" + this.$route.query.userArticleId)
      //   location.reload();
      // }
    },
    getCode: function() {
      // 非静默授权，第一次有弹框
      this.code = '';
      var local = window.location.href; // 获取页面url
      var appid = 'wxe68aaf0c3fe0e629';
      this.code = this.getUrlCode().code; // 截取code
      console.log('截取code' + this.code);
      if (this.code == null || this.code === '') {
        // 如果没有code，则去请求
        window.location.href =
          'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
          appid +
          '&redirect_uri=' +
          encodeURIComponent(local) +
          '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
      } else {
        var that = this;
        $.ajax({
          url: 'https://yokapp.mynatapp.cc/wechat/getInfo',
          type: 'GET',
          async: false,
          data: { code: that.code },
          success: function(data) {
            console.log('getInfo成功' + data);
            localStorage.setItem('openId', data.openId);
            localStorage.setItem('nickname', data.nickname);
            localStorage.setItem('headImgUrl', data.headImgUrl);
            window.location.href = 'https://yok.mynatapp.cc/#/articleDetail?articleId=' +
                that.$route.query.articleId + '&userArticleId=' + that.$route.query.userArticleId + '&article=' +that.$route.query.article;
            // that.openid = data.openId;
            // console.log('设置localStorage成功');
          },
          error: function(xhr, textStatus) {
            console.log('getInfo失败' + textStatus);
          }
        });
      }
    },
    getUrlCode: function() {
      // 截取url中的code方法
      var url = location.search;
      var theRequest = new Object();
      if (url.indexOf('?') != -1) {
        var str = url.substr(1);
        var strs = str.split('&');
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
        }
      }
      return theRequest;
    }
  }
};
</script>

<style scoped></style>
